package com.retch.wenchengmao.recodeauthutils.asyn;

import android.util.Log;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Map;
import cz.msebera.android.httpclient.Header;

/**
 * author:wenchengmao
 * time:  2017/3/28.
 * email: wenchengmao@yeah.net
 */
//asyn网络请求的封装类
public class NetworkUtil {
    public static final String BASE_URL="http://115.159.53.242/lsteach/";
    private static AsyncHttpClient client = new AsyncHttpClient();
    interface NetworkProcess {
        void success(JSONObject jsonObject);
        void success();
        void netError();
        void infoError();
        void empty();
    }

    //通过数组来传递参数
    public static void post(String url, RequestParams params, final NetworkProcess networkProcess)
    {
        client.post(getFullUrl(url),params,new JsonHttpResponseHandler()
        {
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                //处理json对象
                Log.d("response",response.toString());
                networkProcess.success(response);

            }

            public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
               //处理json数组
                Log.d("respomse",response.toString());
                networkProcess.success();
            }

            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse)
            {
                networkProcess.infoError();
            }

            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse)
            {
                networkProcess.infoError();
            }

            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable)
            {
                Log.d("respomse",throwable.toString());
                Log.d("responseString",responseString);
                networkProcess.netError();
            }

        });
    }

    //获取完整网络访问路径
    public static String getFullUrl(String url)
    {
        String fullUrl=BASE_URL+url;
        return fullUrl;
    }

    //通过Map数据结构传递参数
    public static void postByMap(String url, Map<String,String> map,final NetworkProcess networkProcess)
    {
        RequestParams requestParams=new RequestParams(map);
        client.post(url,requestParams,new JsonHttpResponseHandler()
        {
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                //处理json对象
                Log.d("respomse",response.toString());
                networkProcess.success();
            }

            public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                //处理json数组
                Log.d("respomse",response.toString());
                networkProcess.success();
            }

            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse)
            {
                Log.d("respomse",errorResponse.toString());
                networkProcess.infoError();
            }

            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse)
            {
                Log.d("respomse",errorResponse.toString());
                networkProcess.infoError();
            }

            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable)
            {
                Log.d("respomse",throwable.toString());
                networkProcess.netError();
            }
        });
    }

}
